//
//  ViewController.m
//  pullDown
//
//  Created by xingshuai on 2021/4/6.
//

#import "ViewController.h"

#import "XSRefreshView.h"

#define rel_h 80

@interface ViewController () <UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) SDCycleScrollView *cycleView;
@property (nonatomic, strong) UIView *bannerBackView;
@property (nonatomic, strong) XSRefreshView *refreshView;
@property (nonatomic, assign) BOOL isRefreshing;
@property (nonatomic, assign) CGFloat sta_h;

@end

@implementation ViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self creatViewUI];
    
}

-(CGFloat)sta_h {
    _sta_h = ScreenHeight > 736 ? 44 : 20;
    return _sta_h;
}

-(void)creatViewUI {
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
        
    self.bannerBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ban_h)];
    self.table.tableHeaderView = self.bannerBackView;

    self.cycleView.frame = self.bannerBackView.frame;
    [self.bannerBackView addSubview:self.cycleView];
    [self.cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.bannerBackView);
    }];
    
    [self creatRefreshView];
}

#pragma mark - 刷新控件
-(void)creatRefreshView {
    self.refreshView = [[XSRefreshView alloc] initWithFrame:CGRectMake(0, 44, ScreenWidth, 40)];
    [self.view insertSubview:self.refreshView aboveSubview:self.table];
    self.refreshView.refreshStyle = RefreshViewStyleLoad;
    self.refreshView.refreshOffset = rel_h;
    
    __weak typeof(self)weakSelf = self;
    [self.refreshView startAnimation:^{
        [weakSelf handleData];
    }];
}

-(void)handleData{
    self.isRefreshing = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.isRefreshing = NO;
        //[self.table setContentOffset:CGPointZero animated:YES];
        [self.table setContentOffset:CGPointMake(0, -44) animated:YES];
        [self.refreshView removeAnimation];
    });
}

-(SDCycleScrollView *)cycleView {
    if(!_cycleView) {
        // 轮播图
        _cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage new]];
        _cycleView.autoScrollTimeInterval = 5;
        _cycleView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _cycleView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _cycleView.localizationImageNamesGroup = @[@"hhh", @"hhh", @"hhh", @"hhh"];  // 本地图片
    }
    return _cycleView;
}


-(UITableView *)table {
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.tableFooterView = [UIView new];
    }
    return _table;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 15;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellid = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"百视TV -- %ld",indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"---点击了第%ld张图片", (long)index);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 0) {
        offsetY = 0;
    }
    NSLog(@"off = %lf",offsetY);
    
    // 放大到一定大小不需要继续放大可以在这里设置
    /**
    if (fabs(offsetY) > (self.sta_h + rel_h)) {

    } else {
        [self.cycleView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bannerBackView).with.offset(offsetY);
        }];
    }
    */
    // 下拉放大
    [self.cycleView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bannerBackView).with.offset(offsetY);
    }];
    
    //2 刷新控件设置
    if (!self.isRefreshing) {
        __weak typeof(self)weakSelf = self;
        
        CGFloat refreshOffsetY = -scrollView.contentOffset.y - rel_h;
        [self.refreshView contentOffsetY:refreshOffsetY withDragging:scrollView.isDragging isStyleLoad:^{
            weakSelf.isRefreshing = YES;
            [weakSelf handleData];
        }];
    }
    
    if (self.isRefreshing) {
        [scrollView setContentOffset:CGPointMake(0, -(self.sta_h + rel_h)) animated:NO];
    }
}


@end

